package com.haoting.blogservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoting.blogservice.entity.Blogsubject;
import com.haoting.blogservice.entity.excel.SubjectData;
import com.haoting.blogservice.entity.subject.OneSubject;
import com.haoting.blogservice.entity.subject.TwoSubject;
import com.haoting.blogservice.listener.SubjectExcelListenser;
import com.haoting.blogservice.mapper.BlogsubjectMapper;
import com.haoting.blogservice.service.BlogsubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tingting
 * @since 2020-12-21
 */
@Service
public class BlogsubjectServiceImpl extends ServiceImpl<BlogsubjectMapper, Blogsubject> implements BlogsubjectService {

    @Override
    public void saveSubject(MultipartFile file,BlogsubjectService blogsubjectService) {
        try{
            InputStream in = file.getInputStream();
            baseMapper.deleteall();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListenser(blogsubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    //文章分类列表
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类
        QueryWrapper<Blogsubject> wrapperone = new QueryWrapper<>();
        wrapperone.eq("parent_id","0");
        List<Blogsubject> oneSubjectList = baseMapper.selectList(wrapperone);

        //查询所有二级分类
        QueryWrapper<Blogsubject> wrappertwo = new QueryWrapper<>();
        wrappertwo.ne("parent_id","0");
        List<Blogsubject> twoSubjectList = baseMapper.selectList(wrappertwo);

        //创建List集合，封装最后的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //一级分类封装
        for (int i = 0; i < oneSubjectList.size(); i++) {//遍历
            Blogsubject blogsubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(blogsubject.getId());
//            oneSubject.setTitle(blogsubject.getTitle());
            BeanUtils.copyProperties(blogsubject,oneSubject);//Spring内置工具类，效果同上

            //二级分类封装
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                Blogsubject tSubject = twoSubjectList.get(m);
                if(tSubject.getParentId().equals(blogsubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }

            //把一级分类下面对应的所有二级分类放到一级分类里面
            oneSubject.setChildren(twoFinalSubjectList);
            finalSubjectList.add(oneSubject);
        }


        return finalSubjectList;


    }
}
